// page/category/prolist.js
var utils = require('../../utils/util.js');
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
	data: {
		tabs: ["", "", "", ""],
		activeIndex: 0,
		sliderOffset: 0,
		showDown: 0,
		num1: [],
		productItems: [],  //显示的
		list: [],  //全部的
		status: 0,
		p: 1,
		use: '',
		colour: '',
		genre: '',
		num: '',
	},
	onLoad: function (options) {
		var that = this;
		var app = getApp()
		that.setData({
			use: options['use'],
			colour: options['colour'],
			genre: options['genre'],
			num: options['num'],
		})
		utils.GET({
			url: 'Category/allProlist',
			params: {
				use: that.data.use,
				colour: that.data.colour,
				genre: that.data.genre,
				num: that.data.num,
				id: options.id
			},
			success: function (res) {
				wx.setNavigationBarTitle({
					title: res.data.title
				})
				console.log(res)
				that.setData({
					productItems: res.data.d,
					list: res.data.d,
					num1: res.data.n,
					p: res.data.page
				})
			},
		})

		wx.getSystemInfo({
			success: function (res) {
				that.setData({
					isHide: 1,
					sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
					sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
				});
			}
		});
	},
	onShow: function (options) {
		var that = this;
		var app = getApp()
		that.setData({
			use: options['use'],
			colour: options['colour'],
			genre: options['genre'],
			num: options['num'],
		})
	},


	tabClick: function (e) {
		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: e.currentTarget.id
		});
		if (e.currentTarget.id == 0) {
			this.setData({
				showDown: 1
			});
		} else if (e.currentTarget.id == 1) {
			this.setData({
				showDown: 2
			});
		} else {
			var that = this;
			var k = e.currentTarget.dataset.sas
			let arr = that.data.list;
			var t = that.data.status
			if (t == 0) {
				for (var i = 0; i <= arr.length - 1; i++) {
					for (var j = 0; j < arr.length - 1 - i; j++) {
						if (parseFloat(arr[j][k]) > parseFloat(arr[j + 1][k])) {
							var temp = arr[j];
							arr[j] = arr[j + 1];
							arr[j + 1] = temp;
						}
					}
				}
				that.setData({
					productItems: arr,
					status: 1,
					showDown: 0
				})
			} else {
				for (var i = 0; i <= arr.length - 1; i++) {
					for (var j = 0; j < arr.length - 1 - i; j++) {
						if (arr[j][k] < arr[j + 1][k]) {
							var temp = arr[j];
							arr[j] = arr[j + 1];
							arr[j + 1] = temp;
						}
					}
				}
				that.setData({
					productItems: arr,
					status: 0,
					showDown: 0
				})
			}
		}

	},

	closeDropDown: function (e) {//销量 人气 评价
		var that = this;
		var k = e.currentTarget.dataset.sas
		let arr = that.data.list;

		for (var i = 0; i <= arr.length - 1; i++) {
			for (var j = 0; j < arr.length - 1 - i; j++) {
				if (arr[j][k] < arr[j + 1][k]) {
					var temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
		that.setData({
			productItems: arr,
			showDown: 0
		})
	},

	numlist: function (e) {//销量 人气 评价

		var that = this;
		var k = e.currentTarget.dataset.id
		let arr = that.data.list;
		var list = [];
		for (var i = 0; i <= arr.length - 1; i++) {
			if (arr[i]['num'] == k) {
				list.push(arr[i])
			}
		}
		that.setData({
			productItems: list,
			showDown: 0
		})
	},
	onReachBottom: function () {
		var that = this;
		utils.GET({
			url: 'Category/prolist',
			params: {
				use: that.data.use,
				colour: that.data.colour,
				genre: that.data.genre,
				num: that.data.num,
				p: parseInt(that.data.p) + 1
			},
			success: function (res) {
				console.log(res.data)
				that.setData({
					p: res.data.page,
					list: list,
					productItems: list,
				})
			},
		})
	},

})
